unit FindDeals;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dRoot, DB, StdCtrls, Buttons, ExtCtrls, ComCtrls, cxStyles,
  cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,
  cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  IBCustomDataSet, IBQuery;

type
  TfmFindDeals = class(TdlgRoot)
    rgOptions: TRadioGroup;
    edNumber: TEdit;
    Label1: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Label2: TLabel;
    dbgClients: TcxGrid;
    dbgClientsDBTableView1: TcxGridDBTableView;
    dbgClientsLevel1: TcxGridLevel;
    qRyba: TIBQuery;
    qFind: TIBQuery;
    qFindDEID: TIntegerField;
    qFindDEOFIDSEND: TIntegerField;
    qFindDEOFIDDEST: TIntegerField;
    qFindDEWAYBILL: TIBStringField;
    qFindSCLIENT: TIBStringField;
    qFindRCLIENT: TIBStringField;
    qFindINDDEALS: TIBStringField;
    dbgClientsDBTableView1DEID: TcxGridDBColumn;
    dbgClientsDBTableView1DEOFIDSEND: TcxGridDBColumn;
    dbgClientsDBTableView1DEOFIDDEST: TcxGridDBColumn;
    dbgClientsDBTableView1DEWAYBILL: TcxGridDBColumn;
    dbgClientsDBTableView1SCLIENT: TcxGridDBColumn;
    dbgClientsDBTableView1RCLIENT: TcxGridDBColumn;
    dbgClientsDBTableView1INDDEALS: TcxGridDBColumn;
    BitBtn3: TBitBtn;
    procedure sbFindClientClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fmFindDeals: TfmFindDeals;

implementation

uses DBinv, InvProc, InvTypes;

{$R *.dfm}

procedure TfmFindDeals.BitBtn3Click(Sender: TObject);
begin
  if qFind.Active then qFind.Close;
  qFind.SQL:= qRyba.SQL;
  case rgOptions.ItemIndex of
    0:begin
        if edNumber.Text = '' then Exit;
        qFind.SQL.Add('WHERE D.DEID = :DEID');
        qFind.ParamByName('DEID').AsInteger:= StrToInt(edNumber.Text);
        qFind.Open;
      end;
    1:begin
        if edNumber.Text = '' then Exit;
        qFind.SQL.Add('WHERE D.DEWAYBILL = :DEWAYBILL');
        qFind.ParamByName('DEWAYBILL').AsString:= edNumber.Text;
        qFind.Open;
      end;
    2:begin
        if edNumber.Text = '' then Exit;
        qFind.SQL.Add('WHERE I.DIID = :DIID');
        qFind.ParamByName('DIID').AsInteger:= StrToInt(edNumber.Text);
        qFind.Open;
      end;
    3:begin
        if (Edit1.Text+Edit2.Text+Edit3.Text+Edit4.Text+Edit5.Text) = '' then Exit;
        qFind.SQL.Add('WHERE 1=1');
        if (Edit1.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND1 = :DGIND1');
          qFind.ParamByName('DGIND1').AsString:= Edit1.Text;
        end;
        if (Edit2.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND2 = :DGIND2');
          qFind.ParamByName('DGIND2').AsString:= Edit2.Text;
        end;
        if (Edit3.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND3 = :DGIND3');
          qFind.ParamByName('DGIND3').AsString:= Edit3.Text;
        end;
        if (Edit4.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND4 = :DGIND4');
          qFind.ParamByName('DGIND4').AsString:= Edit4.Text;
        end;
        if (Edit5.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND5 = :DGIND5');
          qFind.ParamByName('DGIND5').AsString:= Edit5.Text;
        end;
        qFind.Open;
      end;
  end;
end;

procedure TfmFindDeals.FormShow(Sender: TObject);
begin
  inherited;
  edNumber.SetFocus;
end;

procedure TfmFindDeals.sbFindClientClick(Sender: TObject);
begin
  if qFind.Active then qFind.Close;
  qFind.SQL:= qRyba.SQL;
  case rgOptions.ItemIndex of
    0:begin
        if edNumber.Text = '' then Exit;
        qFind.SQL.Add('WHERE D.DEID = :DEID');
        qFind.ParamByName('DEID').AsInteger:= StrToInt(edNumber.Text);
        qFind.Open;
      end;
    1:begin
        if edNumber.Text = '' then Exit;
        qFind.SQL.Add('WHERE D.DEWAYBILL = :DEWAYBILL');
        qFind.ParamByName('DEWAYBILL').AsString:= edNumber.Text;
        qFind.Open;
      end;
    2:begin
        if edNumber.Text = '' then Exit;
        qFind.SQL.Add('WHERE I.DIID = :DIID');
        qFind.ParamByName('DIID').AsInteger:= StrToInt(edNumber.Text);
        qFind.Open;
      end;
    3:begin
        if (Edit1.Text+Edit2.Text+Edit3.Text+Edit4.Text+Edit5.Text) = '' then Exit;
        qFind.SQL.Add('WHERE 1=1');
        if (Edit1.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND1 = :DGIND1');
          qFind.ParamByName('DGIND1').AsString:= Edit1.Text;
        end;
        if (Edit2.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND2 = :DGIND2');
          qFind.ParamByName('DGIND2').AsString:= Edit2.Text;
        end;
        if (Edit3.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND3 = :DGIND3');
          qFind.ParamByName('DGIND3').AsString:= Edit3.Text;
        end;
        if (Edit4.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND4 = :DGIND4');
          qFind.ParamByName('DGIND4').AsString:= Edit4.Text;
        end;
        if (Edit5.Text <> '') then begin
          qFind.SQL.Add('AND G.DGIND5 = :DGIND5');
          qFind.ParamByName('DGIND5').AsString:= Edit5.Text;
        end;
        qFind.Open;
      end;
  end;
end;

end.
